﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Web.UI.WebControls;

namespace DevExpress.MailClient.Win.TasksManager
{
    public partial class ucWorkFlowStepsDisplay : PRO_DOCS.BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {
        private PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_WORKFLOW _cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW;
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_WORKFLOW CURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW
        {
            get { return _cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW; }
            set
            {
                _cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW = value;
                //Load the workflow steps for the the current document type.
                if (_cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW != null)
                {
                    Dictionary<Guid, PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG> ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG = CURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOGs.ToDictionary(k => k.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_STEP_ID.Value, v => v);

                    List<clsWorkflowStep> clsworkFlowSteps = new List<clsWorkflowStep>();

                    List<PRO_DOCS.DAL.Database.APPLICATION_WORKFLOW_STEP> workflow_steps =
                        (
                            from steps in PRO_DOCSDatabaseDataContext.APPLICATION_WORKFLOW_STEPs
                            where steps.APPLICATION_WORFLOW_ID.Value.Equals
                            (_cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW.APPLICATION_WORFLOW_ID.Value)
                            orderby steps.APPLICATION_WORKFLOW_STEP_NUMBER.Value
                            select steps
                        ).ToList();

                    for (int i = 0; i < workflow_steps.Count; i++)
                    {
                        clsWorkflowStep singleWorkFlow = new clsWorkflowStep();


                        singleWorkFlow.APPLICATION_WORKFLOW_STEP = workflow_steps[i];

                        //1. get the ROW ID
                        singleWorkFlow.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_STEP_ID = workflow_steps[i].APPLICATION_WORKFLOW_STEP_ID;

                        //2. GET the values if it has log

                        if (_cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW.APPLICATION_WORKFLOW_INSTANCE_NEXT_STEP_ID.Equals(workflow_steps[i].APPLICATION_WORKFLOW_STEP_ID))
                        {
                            //ge the image if it is in the next step ID
                            singleWorkFlow.APPLICATION_WORKFLOW_ACTION = 3;
                        }
                        else
                        {
                            //the default image for non answered (BLANK)
                            singleWorkFlow.APPLICATION_WORKFLOW_ACTION = -1;

                            //if it has log we get it's ICON
                            if (ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG != null)
                            {
                                if (ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG.ContainsKey(workflow_steps[i].APPLICATION_WORKFLOW_STEP_ID))
                                {
                                    singleWorkFlow.APPLICATION_WORKFLOW_ACTION = ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG[workflow_steps[i].APPLICATION_WORKFLOW_STEP_ID].ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_ACTION_ID.Value;
                                    //we get signed by Name
                                    singleWorkFlow.SIGNED_BY_USER_NAME = ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG[workflow_steps[i].APPLICATION_WORKFLOW_STEP_ID].LOGIN_USER.EMPLOYEE_FULL_NAME;
                                    //get the Notes
                                    singleWorkFlow.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_NOTES = ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG[workflow_steps[i].APPLICATION_WORKFLOW_STEP_ID].ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_NOTES;
                                    //we get the workflow LOG data
                                    singleWorkFlow.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG = ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG[workflow_steps[i].APPLICATION_WORKFLOW_STEP_ID];
                                    //we get the log date
                                    singleWorkFlow.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_CREATION_DATE = ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG[workflow_steps[i].APPLICATION_WORKFLOW_STEP_ID].ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_CREATION_DATE.Value;
                                }
                            }
                        }


                        clsworkFlowSteps.Add(singleWorkFlow);
                    }

                    gcApplicationWorkFlowSteps.DataSource = clsworkFlowSteps;

                    if (clsworkFlowSteps.Count > 0)
                    {
                        gvApplicationWorkFlowSteps.SelectRow(0);
                        LOAD_WORKFLOW_STEP_DATA();
                    }
                    //
                    //gcApplicationWorkFlowSteps.DataSource = ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG;
                }
            }
        }

        public ucWorkFlowStepsDisplay()
        {
            InitializeComponent();
        }
        clsWorkflowStep ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOGSTEP
        { get; set; }
        private void gvApplicationWorkFlowSteps_FocusedRowChanged(object sender, XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

            LOAD_WORKFLOW_STEP_DATA();
        }

        private void LOAD_WORKFLOW_STEP_DATA()
        {
            lstReplyAttachments.Items.Clear();

            if (gvApplicationWorkFlowSteps.FocusedRowHandle > -1)
            {
                ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOGSTEP = gvApplicationWorkFlowSteps.GetFocusedRow() as clsWorkflowStep;
                memoReplyMessage.Text = ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOGSTEP.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_NOTES;

                LOAD_WORKFLOW_STEP_ATTACHMENT();
            }
            else
            {
                memoReplyMessage.Text = string.Empty;
            }
        }

        private void LOAD_WORKFLOW_STEP_ATTACHMENT()
        {

            if (ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOGSTEP.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG != null)
            {
                List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_ATTACHMENT> fileAttachments = (from attachment in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_ATTACHMENTs
                                                                                                               where attachment.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_ID.Value.Equals
                                                                                                               (
                                                                                                                    ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOGSTEP.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_ID
                                                                                                               )
                                                                                                               select attachment).ToList();
                for (int i = 0; i < fileAttachments.Count; i++)
                {
                    ListItem liSingleAttachment = new ListItem();
                    liSingleAttachment.Text = fileAttachments[i].ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_ATTACHMENT_NAME;
                    liSingleAttachment.Value = fileAttachments[i].ARCHIVE_DEPARTMENT_FILE_WORKFLOW_LOG_ATTACHMENT_ID.ToString();

                    lstReplyAttachments.Items.Add(liSingleAttachment);
                }

            }
        }

        private void gcApplicationWorkFlowSteps_Click(object sender, EventArgs e)
        {

        }

        private void lstReplyAttachments_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lstReplyAttachments.SelectedItem != null)
            {
                ListItem liSelectedAttachment = (ListItem)lstReplyAttachments.SelectedItem;

                new Forms.frmPhysicalFileViewer
                    (
                        liSelectedAttachment.Text,
                        liSelectedAttachment.Value,
                        new PRO_DOCS.BLL.WIN.SystemSettings.SystemSettings().APPLICATION_WORKFLOW_SAVING_FOLDERS
                    ).ShowDialog();


            }
        }
    }
}
